package com.example.a10928.weifriend.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.a10928.weifriend.R;

public class SwitchLayout  extends RelativeLayout {

    TextView tv_title;
    int index=0;
    private SelectInterface selectInterface;


    public SwitchLayout(Context context) {
        super(context);
    }

    public SwitchLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_switch, this);
        TextView tv_one = (TextView) findViewById(R.id.tv_income);
        tv_title = (TextView) findViewById(R.id.tv_vip);


        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SwitchLayout);
        String one = typedArray.getString(R.styleable.SwitchLayout_SwitchLayout_one);
        String two = typedArray.getString(R.styleable.SwitchLayout_SwitchLayout_two);


        typedArray.recycle();//释放资源

        tv_one.setText(one);
        tv_title.setText(two);

        tv_one.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(index!=0){
                    index=0;
                    tv_one.setBackgroundResource(R.drawable.left_rank_switch_bg);
                    tv_title.setBackgroundResource(R.drawable.right_rank_switch_bg_unselect);
                    tv_one.setTextColor(context.getResources().getColor(R.color.white));
                    tv_title.setTextColor(context.getResources().getColor(R.color.green));
                    if(selectInterface!=null){
                        selectInterface.select(index);
                    }
                }
            }
        });

        tv_title.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(index!=1){
                    index=1;
                    tv_one.setBackgroundResource(R.drawable.left_rank_switch_bg_unselect);
                    tv_title.setBackgroundResource(R.drawable.right_rank_switch_bg);
                    tv_one.setTextColor(context.getResources().getColor(R.color.green));
                    tv_title.setTextColor(context.getResources().getColor(R.color.white));
                    if(selectInterface!=null){
                        selectInterface.select(index);
                    }
                }
            }
        });

    }

    public interface SelectInterface{
        public void select(int index);
    }

    public SelectInterface getSelectInterface() {
        return selectInterface;
    }

    public void setSelectInterface(SelectInterface selectInterface) {
        this.selectInterface = selectInterface;
    }
}
